package secondFragment.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import common.Constant;
import secondFragment.bean.OnlineAskBean;
import secondFragment.bean.VolunteerListBean;
import utils.MyLogUtils;
import view.MyImageLoader;
import volunteer.activity.Volunteer;

/**
 * Created by Administrator on 2017/2/21.
 * 在线提问
 */

public class OAExpandableLvAdapter extends BaseExpandableListAdapter {
    private Map<String, List<VolunteerListBean.ConsulterListBean>> dataset;
    private Context context;
    private ArrayList<OnlineAskBean.CollegeListBean> parentList;

    public OAExpandableLvAdapter(Context context, Map<String, List<VolunteerListBean.ConsulterListBean>> dataset, ArrayList<OnlineAskBean.CollegeListBean> parentList) {
        this.context = context;
        this.dataset = dataset;
        this.parentList = parentList;
    }

    @Override
    public int getGroupCount() {
        if (parentList != null) {
            return dataset.size();
        } else {
            return 0;
        }
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (parentList.size() > 0) {
            if (dataset.get(parentList.get(groupPosition).getSchool_name()) != null) {
                return dataset.get(parentList.get(groupPosition).getSchool_name()).size();
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (dataset.size() > 0) {
            return dataset.get(groupPosition);
        } else {
            return null;
        }
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (dataset.size() > 0) {
            return dataset.get(parentList.get(groupPosition).getSchool_name()).get(childPosition);
        } else {
            return null;
        }
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    //  获得父项显示的view
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        MyGroupHolder myGroupHolder;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.oa_expandable_lv_groud_item, null);
            myGroupHolder = new MyGroupHolder();
            convertView.setTag(myGroupHolder);
        } else {
            myGroupHolder = (MyGroupHolder) convertView.getTag();
        }
        myGroupHolder.groupIcon = (ImageView) convertView.findViewById(R.id.oa_lv_group_icon);
        myGroupHolder.groupName = (TextView) convertView.findViewById(R.id.oa_lv_group_title);
        myGroupHolder.childNum = (TextView) convertView.findViewById(R.id.oa_lv_group_num);
        myGroupHolder.groupName.setText(parentList.get(groupPosition).getSchool_name() + "");
        myGroupHolder.childNum.setText(parentList.get(groupPosition).getCount() + "");
        if (isExpanded) {
            myGroupHolder.groupIcon.setImageResource(R.mipmap.oa_expandlv_show_icon);
        } else {
            myGroupHolder.groupIcon.setImageResource(R.mipmap.oa_expandlv_close_icon);
        }


        return convertView;
    }

    //  获得子项显示的view
    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        MyChildHolder myChildHolder;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.oa_expandable_lv_child_item, null);
            myChildHolder = new MyChildHolder();
            myChildHolder.icon = (RoundedImageView) convertView.findViewById(R.id.oa_listView_item_icon);
            myChildHolder.name = (TextView) convertView.findViewById(R.id.oa_listView_item_name);
            myChildHolder.title = (TextView) convertView.findViewById(R.id.oa_listView_item_title);
            myChildHolder.more = (TextView) convertView.findViewById(R.id.oa_listView_item_more);
            myChildHolder.ask = (TextView) convertView.findViewById(R.id.oa_listView_item_ask);
            myChildHolder.oa_listView_item_vip_ima = (ImageView) convertView.findViewById(R.id.oa_listView_item_vip_ima);
            convertView.setTag(myChildHolder);
        } else {
            myChildHolder = (MyChildHolder) convertView.getTag();
        }

        final List<VolunteerListBean.ConsulterListBean> arrayList = dataset.get(parentList.get(groupPosition).getSchool_name());
        String free_ask = context.getResources().getString(R.string.free_ask);
        String pay_ask = context.getResources().getString(R.string.pay_ask);
        int color_blue = context.getResources().getColor(R.color.common_color);
        int color_orange = context.getResources().getColor(R.color.orange);

        if (arrayList != null && arrayList.size() > 0) {

            MyImageLoader.showImage(arrayList.get(childPosition).getHeadimgurl(), myChildHolder.icon);
            myChildHolder.name.setText("" + arrayList.get(childPosition).getName());
            String Major = arrayList.get(childPosition).getMajor();
            myChildHolder.more.setText(Major);
            switch (arrayList.get(childPosition).getType()) {
                case Constant.VOLUNTEER_TYPE_VIP:
                    myChildHolder.ask.setText(pay_ask);
                    myChildHolder.ask.setTextColor(color_blue);
                    myChildHolder.oa_listView_item_vip_ima.setVisibility(View.GONE);
                    if (arrayList.get(childPosition).getBrief() != null) {
                        myChildHolder.title.setText("" + arrayList.get(childPosition).getBrief());
                    }
                    myChildHolder.ask.setBackgroundResource(R.drawable.shape_corner);
                    break;
                case Constant.VOLUNTEER_TYPE_OFFICIAL:
                    //官方志愿者
                    myChildHolder.ask.setText(free_ask);
                    myChildHolder.ask.setTextColor(color_orange);
                    myChildHolder.oa_listView_item_vip_ima.setVisibility(View.VISIBLE);
                    myChildHolder.title.setText("官方咨询员");
                    myChildHolder.ask.setBackgroundResource(R.drawable.shape_corner_orange);
                    break;
                case Constant.VOLUNTEER_TYPE_NORMAL:
                default:
                    myChildHolder.ask.setText(free_ask);
                    myChildHolder.ask.setTextColor(color_orange);
                    myChildHolder.oa_listView_item_vip_ima.setVisibility(View.GONE);
                    if (arrayList.get(childPosition).getGrade() != null) {
                        myChildHolder.title.setText("" + arrayList.get(childPosition).getGrade());
                    }
                    myChildHolder.ask.setBackgroundResource(R.drawable.shape_corner_orange);
                    break;
            }
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Volunteer.class);
                intent.putExtra("type", arrayList.get(childPosition).getType());
                intent.putExtra("id", arrayList.get(childPosition).getId());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    //  子项是否可选中，如果需要设置子项的点击事件，需要返回true
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class MyGroupHolder {
        ImageView groupIcon;
        TextView groupName;
        TextView childNum;
    }

    class MyChildHolder {
        RoundedImageView icon;
        TextView name;
        TextView title;
        TextView more;
        TextView ask;
        ImageView oa_listView_item_vip_ima;
    }

}
